$(function() {
	music_index.run();
});

var music_index = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();
	},

	_init_ : function() {		
		this.scrollPage = 1;		
		this.contextPath = $("#contextPath").val();	
		this.vdInstalled = vd.check();
		this.itemUrl = '';
		this.title = '';
		this.period = $("#period").val() || "daily";			
		this.genre = $("#genre").val() || 'all';
		this.initial = $("#alpha").val() || '';
		
	},

	_start_ : function() {
		side_pub._init_side_genre_decade(this);
		
		var loc = location.href;		
		var index = loc.indexOf("#");
		var params = null;
		if(index > 0){
			params = loc.substring(index + 1);
		}		
		if(params){
			this._query_musics(params);
		}	
		
		this._adjust_layout();
		
		$("#side_menu > ul > li").eq(0).addClass("curr");
	},
	
	_init_events : function() {		
		$("#gotoTop").easeup();	
		
		$("#genre_div > div > ul > li > a").click(function() {
			var $this = $(this);
			music_index.genre = $this.attr("genre");
			
			$("#music_genre_ul > li > a").removeClass("selected");			
			$this.addClass("selected");	
			
			$("#alpha_div > a.initial").removeClass("selected").eq(0).addClass("selected");			
			
			music_index.scrollPage = 1;					
			music_index.initial = "";
			music_index.sort = "";
			music_index._query_musics();			
			
			return false;
		});

		$("#side_category_div > ul > li > a").click(function() {
			var $this = $(this);
			music_index.popular = $this.attr("popular");
			
			$("#side_category_div > ul > li > a").removeAttr("class");
			$this.addClass("selected");
			
			$("#alpha_div > a.initial").removeClass("selected").eq(0).addClass("selected");	
			
			music_index.scrollPage = 1;
			music_index.initial = "";
			music_index.sort = "";
			
			music_index._query_musics();		
			return false;
		});
		
		$("#alpha_div > a.initial").click(function() {
			var $this = $(this);
			var temp = $this.text();
			$("#alpha_div").find("a").removeClass("selected");	
			$this.addClass("selected");	
			
			if ("Popular" == temp) {
				music_index.sort = "popular";
				music_index.initial = "";
			} else {
				music_index.initial = $(this).text();
			}
			
			music_index._query_musics();
			return false;
		});
		
		$(window).wresize(function() {
			music_index._adjust_layout();
		});
		
		$("#open_side_btn").click(function(){
			music_index._adjust_layout("open");
		});
		
		$("#close_side_btn").click(function(){
			music_index._adjust_layout();
		});
		
		music_index._scroll_add_content_event();
		
		this._bind_image_events($("#music_list_div"));
	},
	
	_bind_image_events: function($div){
		$("div.mi_box > div.img", $div).mouseenter(function(){
			$(this).children('div.tc_btn').fadeIn("slow");
		}).mouseleave(function(){
			$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
		});
		
		$("div.mi_box a", $div).click(function(){				
			var href = $(this).attr("href");		
			if(href == 'javascript:;'){
				return;
			}
			var data = [];
			if(music_index.genre){
				data.push("genre=" + music_index.genre);
			}
			if(music_index.popular){
				data.push("period=" + music_index.popular);
			}
			
			if(data.length > 0){
				href += "#" + data.join("&");
			}
			location.href = href;
			return false;
		});
	},
	
	_bind_tips_event : function() {
		$(".btn_down4").click(function() {
			music_index.itemUrl = $(this).attr("itemUrl");
			music_index.title = $(this).attr("itemTitle");
			if (!music_index.vdInstalled) {
				music_index.$alertDialog.dialog({
					submit : function() {
						vd.keep();
						_gaq.push([ '_trackEvent', 'Music', 'Download', music_index.itemUrl ]);
						vd.go(music_index.itemUrl, music_index.title);
						music_index.vdInstalled = true;
					}
				});
				music_index.$alertDialog.dialog('open');
			} else {
				_gaq.push([ '_trackEvent', 'Music', 'Download', music_index.itemUrl ]);
				vd.go(music_index.itemUrl, music_index.title);
			}
			return false;
		});
	},
	
	_scroll_add_content_event: function(){
	    $.scroll(function(){
	    	music_index._add_content();
	    });
	},
	
	_query_musics : function(params) {
		var url = this.contextPath + "/world/music/video/query";
		var data = null;
		if (params) {
			data = params;
		} else {
			data = this._gather_params();
		}		
		this._change_location_value(data);	
		
		$.get(url, data, function(result) {
			result = music_index._bind_fly_images_events(result);
			$("#music_list_div").html(result);			
			music_index._adjust_layout();		
		});		
	},
	
	_bind_fly_images_events: function(result){
		var $div = $("<div>" + result + "</div>");
		this._bind_image_events($div);
		return $div.children();
	},

	_change_location_value : function(data) {
		var loc = location.href;
		loc = loc.substring(0,loc.indexOf("#"));
		if(data){
			loc += "#" + data;
		}
		location.href = loc;
	},

	_gather_params : function() {
		var params = [];
		if (this.initial) {
			params.push("initial=" + this.initial);
		}
		if (this.genre) {
			params.push("genre=" + this.genre);
		}				
		if (this.sort) {
			params.push("sort=" + this.sort);
		}		
		if (this.popular) {
			params.push("period=" + this.popular);
		}		
		if(params.length > 0){
			return params.join("&");
		}		
	},
	
	_add_content: function(){	
        if($("#pageCount").val() > 1 && this.scrollPage < $("#pageCount").val()){
        	this.scrollPage += 1;        	
			var data = this._gather_params(); 
			if(data){
				data += "&page=" + this.scrollPage;
			}else{
				data ="page=" + this.scrollPage;
			}			
			var url = this.contextPath + "/world/music/video/query";
			$.ajax({
				url: url,
				data: data,
				cache: false,
				beforeSend: function(){
					$(".loadingBox").show();
				},
				success: function(result){				
					result = music_index._bind_fly_images_events(result);
					$("#music_list_div").append(result);
					$(".loadingBox").hide();					
					music_index._adjust_layout();					
				}
			});
        }else if($("#pageCount").val()== this.scrollPage){
        	$("#music_list_div").append("<div class='clear'/>");
        }
	},
	
	_adjust_layout: function(action){
		var boxWidth = $("#music_list_div").width()-5;
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/220);
		var paddingValue = 20 + (boxWidth - 220*account + 20)/(account-1);
		var flag = 1;
		$("#music_list_div > div.m_box3").each(function(){
			$(this).css("padding-right",0);
			if(flag < account){
				if(flag == 1){
					$(this).css("padding-left",0);
					flag++;
				}else{
					$(this).css("padding-left",paddingValue);
					flag++;
				}
			}else{
				$(this).css("padding-left",paddingValue);
				flag = 1;
			}
		});
	}
};